#include "boolexp.h"

BoolExp::BoolExp(QString str):_expStr(str)
{    
}


void BoolExp::Interpret(){
    if(DEBUG){
        qDebug("BoolExp::Interpret()\n");
    }
    if(!InterpretAnd())
        if(!InterpretOR())
            InterpretBase();
}

bool BoolExp::InterpretBase(){
    if(DEBUG){
        qDebug("BoolExp::InterpretBase()\n");
    }
    if(_expStr == "")
        return true;

    _type = BaseExpType;
    _pLeftExp = new BaseExp(_expStr);
    return true;
}

bool BoolExp::InterpretOR(){
    if(DEBUG){
        qDebug("BoolExp::InterpretOR\n");
    }
    if(!_expStr.contains("||"))
        return false;
    if(DEBUG){
        qDebug("BoolExp::InterpretOR contains ||\n");
    }
    _type = OrExpType;
    QStringList lStrList = _expStr.split("||");
    CompositeExp *pL = new CompositeExp(lStrList.at(0));
    int index = _expStr.indexOf("||");
    QString rStr = _expStr.right(_expStr.length() - index -2);
    CompositeExp *pR = new CompositeExp(rStr);
    _pLeftExp = new OrExp(pL, pR);
    return true;
}

bool BoolExp::InterpretAnd(){
    if(DEBUG){
        qDebug("BoolExp::InterpretAnd \n");
    }
    if(!_expStr.contains("&&"))
        return false;
    if(DEBUG){
        qDebug("BoolExp::InterpretAnd contains &&\n");
    }

    _type = AndExpType;
    QStringList lStrList = _expStr.split("&&");
    CompositeExp *pL = new CompositeExp(lStrList.at(0));
    int index = _expStr.indexOf("&&");
    QString rStr = _expStr.right(_expStr.length() - index -2);
    CompositeExp *pR = new CompositeExp(rStr);
    _pLeftExp = new AndExp(pL, pR);
    return true;
}
